let App = getApp()

Page({
  data: {
      activeIndex: 0,
      navList: [],
      order: {},
      prompt: {
        hidden: !0,
        icon: '../../../assets/images/iconfont-order-default.png',
        title: '您还没有相关的订单',
        text: '可以去看看有哪些想买的',
      },
  },
  onLoad (option) {

    let userId = wx.getStorageSync('userId')
    let navList = [{
      name: '全部订单',
      id: 'ALL',
    }, {
      name: '待支付订单',
      id: '0',
    }, {
      name: '已支付订单',
      id: '1',
    }, {
      name: '已完成订单',
      id: '2',
    }, {
      name: '已退款订单',
      id: '3',
    }]
    let activeIndex = 0
    navList.forEach((item, index) => {
      if (option.stauts == item.id) {
        activeIndex = index
      }
    })
    if (option.back == 1) {
      this.setData({back: option.back})
    }
    this.setData({
      userId,
      navList,
      activeIndex,
      'order.params.orderStatus': option.stauts,
    })
    this.onPullDownRefresh()
  },
  onUnload () {
    if (this.data.back == 1) {
      wx.reLaunch({
        url: '/pages/user/user'
      })
    }
  },
  initData () {
    let {order, userId} = this.data
    let params = order && order.params
    let orderStatus = params.orderStatus
    this.setData({
      order: {
        items: [],
        total: '',
        params: {
          buyerId: userId,
          page : 1,
          pageCount: 10,
          orderStatus: orderStatus,
        }
      }
    })
  },
  navigateTo (e) { // 订单详情
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/order/detail/index?id=${id}&orderType=0`
    })
  },
  toEvalute (e) {
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/evaluate/index?id=${id}`
    })
  },
  getList (cb) {
    let order = this.data.order
    let params = order.params
    App.HttpService.prodOrderList(params).then(res => {
      cb && cb()
      let data = res.data
      if (data.code == 200) {
        order.items = [...order.items, ...data.data.list]
        if (data.data.list.length > 0) {
          params.page = params.page + 1
          order.total = data.data.counts
        }
        this.setData({
          order: order,
          'prompt.hidden': order.items.length,
        })
      }
    }, e => {
      cb && cb()
    })
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.initData()
    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom() {
    let order = this.data.order
    if (order.items.length === order.total) {
      return
    }
    this.getList()
  },
  onTapTag(e) {
    let type = e.currentTarget.dataset.type
    let index = e.currentTarget.dataset.index
    this.initData()
    this.setData({
        activeIndex: index,
        'order.params.orderStatus': type,
    })
    this.getList()
  },
  toDel (e) { // 删除订单
    let { id } = e.currentTarget.dataset
    wx.showModal({
      title: '删除',
      content: '确定是否删除此订单',
      success: (res) => {
        if (res.confirm) {
          this.orderDel(id)
        }
      }
    })
  },
  orderDel (id) { // 删除订单
    App.HttpService.intOrderDel({id: id}).then(res => {
      this.initData()
      this.getList()
    })
  },
})